package com.goethe.translator;

import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;

public class DieDerDas extends Activity {
	private final static int GERMAN_GENDER = 2;
	
	private GUIUtils guiUtils = new GUIUtils();
	Cursor queryResult = null;
	private int counterCorrect = 0;
	private int counterIncorrect = 0;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_die_der_das);
        
        getRandomWord();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_die_der_das, menu);
        return true;
    }
    
    private void getRandomWord(){
    	// We make the query to the database
    	TranslationsOpenHelper transOpenHelper = new TranslationsOpenHelper(getApplicationContext());
    	SQLiteDatabase db = transOpenHelper.getReadableDatabase();
    	
		String table = TranslationsOpenHelper.TRANSLATOR_TABLE_NAME;
		String[] columns = null;
		String selection = null;
		String[] selectionArgs = null;
		String groupBy = null;
		String having = null;
		String orderBy = null;
		
		queryResult = db.query(table + " Order BY RANDOM() LIMIT 1", columns, selection, selectionArgs, groupBy, having, orderBy);
		
		guiUtils.showDieDerDasWord(queryResult, this);
		
		// Closing the db object to avoid memory leaks
		db.close();
    }
    
    public void selectNext(View view){
    	getRandomWord();
    }
    
    public void selectFinish(View view){
    	guiUtils.showDieDerDasResults(counterCorrect, counterIncorrect, this);
    }
    
    public void selectDie(View view){
    	selectOption("die");
    }
    
    public void selectDer(View view){
    	selectOption("der");
    }
    
    public void selectDas(View view){
    	selectOption("das");
    }
    
    private void selectOption(String option){
    	if(option.equalsIgnoreCase(queryResult.getString(GERMAN_GENDER))){
    		// We show the "correct" message
    		guiUtils.showDieDerDasCorrect(this);
	        
	        // We increment the correct counter
	        counterCorrect+=1;
    	}else{
    		// We show the "incorrect" message
    		guiUtils.showDieDerDasIncorrect(this);
	        
	        // We increment the correct counter
	        counterIncorrect+=1;
    	}
    	
    	guiUtils.showDieDerDasSelectOption(queryResult, this);
    }
}
